package com.usian.controller;

import com.usian.SearchItemFeign;
import com.usian.pojo.SearchItem;
import com.usian.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/frontend/searchItem")
public class SearchItemWebController {

    @Autowired
    SearchItemFeign searchItemFeign;

    /**
     * 商品搜索
     *
     * @param q
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/list")
    public List<SearchItem> searchItemList(@RequestParam String q, @RequestParam(defaultValue = "1") Integer pageNum,
                                           @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            List<SearchItem> list = searchItemFeign.searchItemList(q, pageNum, pageSize);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 商品库导入索引库  mysql导入ES
     *
     * @return
     */
    @RequestMapping("/importAll")
    public Result importAll() {
        Boolean aBoolean = searchItemFeign.importAll();
        if (aBoolean) {
            return Result.ok("导入成功");
        }
        return Result.error("导入失败");
    }

}
